/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nus.application.web.review;

import com.nus.application.ejb.review.ReviewAssignmentEjbLocal;
import com.nus.application.web.base.BaseAction;
import com.nus.application.web.review.form.ReviewAssesmentForm;
import com.nus.dataaccess.dao.review.ReviewAssignment;
import com.nus.dataaccess.dao.review.ReviewResult;
import com.opensymphony.xwork2.ValidationAware;
import java.util.HashMap;
import java.util.Map;
import javax.inject.Inject;

/**
 *
 * @author robin.foe
 */
public class ReviewAssesmentMaintenance extends BaseAction implements ValidationAware {

    @Inject
    ReviewAssignmentEjbLocal reviewAssignmentEjb;

    public String doList() {

        System.err.println(" ReviewAssesmentMaintenance do List : Curr Login" + super.getCurrentLoginUser().getId());
        Map<String, Object> filters = new HashMap<String,Object>();
        filters.put("reviewerId", super.getCurrentLoginUser().getId());
        
        this.form.setFullListSize(reviewAssignmentEjb.countTotal(filters));
        this.form.setPageNumber(super.getPage());
        this.getForm().setReviewAssignments(
                reviewAssignmentEjb.paginate(form.calculateStartRow(super.getPage()), form.getObjectsPerPage(), null, false, filters)
                );

        
        return SUCCESS;
    }
    
    public String doMaintain() {
        ReviewAssignment reviewAssignment = reviewAssignmentEjb.getReviewAssignment(this.getForm().getReviewAssignment().getId());
        if(reviewAssignment.getReviewResults().isEmpty()){
            ReviewResult reviewResult = new ReviewResult();
            reviewResult.setReviewAssignmentId(reviewAssignment.getId());
            this.getForm().setReviewResult(reviewResult);
        }else{
            this.getForm().setReviewResult(reviewAssignment.getReviewResults().get(0));
        }
        this.getForm().setReviewAssignment(reviewAssignment);
        
        return SUCCESS;
    }
    
    public String doSave() {
        reviewAssignmentEjb.saveReviewResult(this.form.getReviewResult());
        super.addActionMessage(super.getText("information_successfully_saved"));
        return SUCCESS;
    }
    
    public String doProcessed() {
        reviewAssignmentEjb.completeReviewResult(this.form.getReviewResult());
        super.addActionMessage(super.getText("information_successfully_saved"));
        return SUCCESS;
    }
    

    private ReviewAssesmentForm form = new ReviewAssesmentForm();
    public ReviewAssesmentForm getForm() {return form;}
    public void setForm(ReviewAssesmentForm form) {this.form = form;}
    
    @Override
    public void validate(){
    }

}
